// Copyright (c) Microsoft Corporation.  All rights reserved.
#include "Common.h"

CUserInfo::CUserInfo() :
    m_pSID(NULL)
{
}

CUserInfo::~CUserInfo()
{
    if (m_pSID)
    {
        LocalFree(m_pSID);
        m_pSID = NULL;
    }
}

HRESULT CUserInfo::Initialize(const SID *pSID)
{
    HRESULT hr = S_OK;

    if (!pSID)
    {
        return E_POINTER;
    }

    if (!IsValidSid((PSID)pSID))
    {
        return E_INVALIDARG;
    }

    DWORD cbSID = GetLengthSid((PSID)pSID);

    m_pSID = (SID*)LocalAlloc(LMEM_FIXED, cbSID);

    if (m_pSID)
    {
        if (!CopySid(cbSID, m_pSID, const_cast<SID*>(pSID)))
        {
            hr = HRESULT_FROM_WIN32(GetLastError());
        }

        if (FAILED(hr))
        {
            LocalFree(m_pSID);
            m_pSID = NULL;
        }
    }
    else
    {
        hr = E_OUTOFMEMORY;
    }

    return hr;
}

